//商品分类商品展示页面
//author：王梦阳

var userId = localStorage.getItem("user"); //用户id 

let getItem = {
	version: 'v1.0',
	getItemInfo: function() {
		var data = eval('(' + localStorage.dataset + ')');
		console.log(data);
		let htmlContent = ``;
		if(data.data.length!=0){
			for (var i = 0; i < data.data.length; i++) {
				console.log(data.data[0].item_img);
				htmlContent +=
					`<span class="item" id="${i}">
						<img  class="item-image" src="${data.data[i].item_img}"/>
						<div class="item-content">
							<span class="item-title">${data.data[i].item_name}</span>
							<span class="item-shop">${data.data[i].item_brand}</span>
							<span class="item-price">￥${data.data[i].item_price}</span>
						</div>
					</span>`
			}
			$("#item-list").html(htmlContent);
			// 实现点击搜索到的商品跳转到商品详情页并将记录一次商品浏览
			for (var j = 0; j < data.data.length; j++) {
				document.getElementById(j).addEventListener('tap', function() {
					let itemId = data.data[this.id].item_id;
					console.log(itemId);
					$.ajax({
						type: "POST",
						dataType: "json",
						async: false,
						contentType: "application/json",
						url: "http://39.98.158.247:8040/browse/insert?userId=" + userId + "&itemId=" + itemId,
						success: function() {
							console.log("insert success");
						},
						error: function() {
							console.log("request error");
						}
					})
					window.open("search-item.html", "_blank");
					localStorage.status = 1;
					localStorage.dataset = JSON.stringify(data);
					localStorage.id = this.id;
				});
			}
		}else{
			htmlContent+=
			`<div id="category-no-item">该类别暂时还没有商品哦</div>`;
			$("#no-item").html(htmlContent);
		}
	}
}

window.onload = function() {
	getItem.getItemInfo();
}
